package elitech.vietnam.weatherfashion.config;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Environment;
import android.widget.ImageView;

public class ImageHelper {
	
	public static final String THUMB_PATH = Environment.getExternalStorageDirectory() + "/_camera_thumb.jpg";

	public static int calculateReqSize(int size, int reqsize) {
		if (size > reqsize)
			return reqsize;
		else
			return size;
	}
	
	public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
	    // Raw height and width of image
	    final int height = options.outHeight;
	    final int width = options.outWidth;
	    int inSampleSize = 1;
	
	    if (height > reqHeight || width > reqWidth) {
	
	        final int halfHeight = height / 2;
	        final int halfWidth = width / 2;
	
	        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
	        // height and width larger than the requested height and width.
	        while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
	            inSampleSize *= 2;
	        }
	    }
	
	    return inSampleSize;
	}
	
	public static boolean rotateImage(String src, String des) {
		Utils.Log("Rotate Bitmap");
		try {
			// Decode by options to get image information
			Options options = new Options();
			options.inJustDecodeBounds = true;
			BitmapFactory.decodeFile(src, options);
			int imageHeight = options.outHeight;
			int imageWidth = options.outWidth;
			//Calculate sample size of bitmap
			float scale;
			
			if (imageWidth > imageHeight) 
				scale = ((float) calculateReqSize(imageWidth, 960)) / ((float) imageWidth);
			else 
				scale = ((float) calculateReqSize(imageHeight, 960)) / ((float) imageHeight);
			
			if (scale > 0) {
				imageHeight *= scale;
				imageWidth *= scale;
			}
			Utils.Log(imageWidth + "x" + imageHeight);
			options.inSampleSize = calculateInSampleSize(options, imageWidth, imageHeight);
			options.inJustDecodeBounds = false;
			ExifInterface ei = new ExifInterface(src);
			int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
			int rotate = 0;
			switch(orientation) {
			    case ExifInterface.ORIENTATION_ROTATE_90:
			    	rotate = 90;
			        break;
			    case ExifInterface.ORIENTATION_ROTATE_180:
			        rotate = 180;
			        break;
			    case ExifInterface.ORIENTATION_ROTATE_270:
			    	rotate = 270;
			        break;
			    // etc.
		        default: 
		        	break;
			}
			if (rotate != 0) {
				Bitmap bitmap = BitmapFactory.decodeFile(src, options);
				Matrix matrix = new Matrix();
				matrix.postRotate(rotate);
				Bitmap img = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
				FileOutputStream out = new FileOutputStream(des);
				img.compress(Bitmap.CompressFormat.JPEG, Const.CAMERA_CAPTURE_COMPRESS_PERCENT, out);
				out.close();
				img.recycle();
				bitmap.recycle();
				return true;
			}
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static void displayThumbImage(ImageView image, String path) {
		try {
			Options options = new Options();
			options.inJustDecodeBounds = true;
			BitmapFactory.decodeFile(path, options);
			int imageHeight = options.outHeight;
			int imageWidth = options.outWidth;
			float scale;
			
			if (imageWidth > imageHeight) 
				scale = ((float) calculateReqSize(imageWidth, 320)) / ((float) imageWidth);
			else 
				scale = ((float) calculateReqSize(imageHeight, 320)) / ((float) imageHeight);
			
			if (scale > 0) {
				imageHeight *= scale;
				imageWidth *= scale;
			}
			
			options.inSampleSize = calculateInSampleSize(options, imageWidth, imageHeight);
			options.inJustDecodeBounds = false;
			Bitmap bitmap = BitmapFactory.decodeFile(path, options);
			image.setImageBitmap(bitmap);
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
		}
	}
	
	public static int getRotation(String path) {
		ExifInterface ei;
		try {
			ei = new ExifInterface(path);
		} catch (IOException e) {
			e.printStackTrace();
			return 0;
		}
		int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
		int rotate = 0;
		switch(orientation) {
		    case ExifInterface.ORIENTATION_ROTATE_90:
		    	rotate = 90;
		        break;
		    case ExifInterface.ORIENTATION_ROTATE_180:
		        rotate = 180;
		        break;
		    case ExifInterface.ORIENTATION_ROTATE_270:
		    	rotate = 270;
		        break;
		    // etc.
	        default: 
	        	break;
		}
		return rotate;
	}
	
	public static void saveThumb(String path) {
		File file = new File(path);
		if (!file.exists())
			return;
		try {
			Options options = new Options();
			options.inJustDecodeBounds = true;
			BitmapFactory.decodeFile(path, options);
			int imageHeight = options.outHeight;
			int imageWidth = options.outWidth;
			float scale;
			
			if (imageWidth > imageHeight) 
				scale = ((float) calculateReqSize(imageWidth, 320)) / ((float) imageWidth);
			else 
				scale = ((float) calculateReqSize(imageHeight, 320)) / ((float) imageHeight);
			
			if (scale > 0) {
				imageHeight *= scale;
				imageWidth *= scale;
			}
			options.inSampleSize = calculateInSampleSize(options, imageWidth, imageHeight);
			options.inJustDecodeBounds = false;
			int rotation = getRotation(path);
			Bitmap bitmap = BitmapFactory.decodeFile(path, options);
			if (rotation != 0) {
				Matrix matrix = new Matrix();
				matrix.postRotate(rotation);
				bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
			}
			FileOutputStream out = new FileOutputStream(THUMB_PATH);
			bitmap.compress(Bitmap.CompressFormat.JPEG, Const.CAMERA_CAPTURE_COMPRESS_PERCENT, out);
			out.close();
			bitmap.recycle();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
		}
	}
}
